//引入使用的模块
const gulp = require("gulp"),
	minifyCss = require("gulp-clean-css"),
	uglify = require("gulp-uglify"),
	htmlmin = require("gulp-htmlmin"),
	babel = require("gulp-babel"),
	connect = require("gulp-connect"),
	sass = require("gulp-sass");

//压缩css
gulp.task("css", () => {
	gulp.src("src/css/*.css")
		.pipe(minifyCss())
		.pipe(gulp.dest("dist/css"))
		.pipe(connect.reload());
});

//压缩html
gulp.task("html", function() {
	gulp.src("src/**/*.html")
		.pipe(htmlmin({
			collapseWhitespace: true
		}))
		.pipe(gulp.dest("dist"))
		.pipe(connect.reload());
});

//压缩js
gulp.task("js", () => {
	gulp.src("src/js/*.js")
		.pipe(uglify())
		.pipe(gulp.dest("dist/js"))
		.pipe(connect.reload());
});

//复制任务
gulp.task("copy-images", () => {
	gulp.src("src/images/**/*.*")
		.pipe(gulp.dest("dist/images"));
});

gulp.task("copy-lib", () => {
	gulp.src("src/lib/**/*.*")
		.pipe(gulp.dest("dist/lib"));
});

gulp.task("copy-font", () => {
	gulp.src("src/font/**/*.*")
		.pipe(gulp.dest("dist/font"));
});

gulp.task("copy", ["copy-images", "copy-lib","copy-font"]);

//webserver
gulp.task("conn", () => {
	connect.server({
		root: 'src',
		livereload: true
	});
});

//监视任务
gulp.task("watch", () => {
	gulp.watch("src/css/*.css", ["css"]);
	gulp.watch("src/js/*.js", ["js"]);
	gulp.watch("src/**/*.html", ["html"]);
});

gulp.task("sass", function() {
	gulp.src("src/scss/*.scss")
		.pipe(sass({
			outputStyle: "expanded"
		}))
		.pipe(gulp.dest("dist/css"))
});

gulp.task("watch-sass", function() {
	gulp.watch("src/scss/*.scss", ["sass"]);
});

//默认缺省任务
//gulp.task("default", ["css", "js", "html", "copy", "conn", "watch"]);
gulp.task("default", ["conn"]);